import { _decorator, Button, Component, director, find, Label, Node, Sprite, SpriteFrame } from 'cc';
import { Core } from '../Core/Core';
import { EGeneralSkill } from '../Config/Enum';


const { ccclass, property } = _decorator;

// 武学格子
@ccclass('GeneralSkill')
export class GeneralSkill extends Component {


    ndBg: Node | null = null;
    ndDesc : Node | null = null;
    skillName: EGeneralSkill

    protected onLoad(): void {
        this.ndDesc = this.node.getChildByName("Desc");

        
        


    }

    protected onEnable(): void {
        this.node.on(Node.EventType.TOUCH_END, this.onBtnChooseThis, this);
        this.ndBg = this.node.getChildByName("BG");

        this.skillName = this.node.name as EGeneralSkill; // 假设节点名称是武学名称的一部分
        console.log("GeneralSkill onEnable this.skillName:", this.skillName);
        console.log("GeneralSkill onEnable Core.Instance.currentGeneralPassiveSkill:", Core.Instance.generalData.assignedPassiveSkill);
        console.log("GeneralSkill onEnable Core.Instance.currentGeneralActiveSkill:", Core.Instance.generalData.assignedActiveSkill);

        // 如果Core.Instance.assignedGeneralSkills中有该武学名称，说明该武学已被分配
        // if (Core.Instance.assignedGeneralSkills[this.node.name]) {
        //     this.ndBg.active = true; // 显示边框
        // } else {
        //     this.ndBg.active = false; // 隐藏边框
        // }


        //  检查自己有没有被分配
        if (this.skillName === Core.Instance.generalData.assignedActiveSkill || this.skillName === Core.Instance.generalData.assignedPassiveSkill) {
            this.ndBg.active = true; // 显示边框
        }

    }

    protected onDisable(): void {

        this.node.off(Node.EventType.TOUCH_END, this.onBtnChooseThis, this);
        
    }

    onBtnChooseThis(){
        console.log("点击了选择武学按钮this.node.name", this.node.name);
        console.log("点击了选择武学按钮this.skillName", this.skillName);

        // 如果this.skillName是被动武学
        if(this.skillName === EGeneralSkill.Fengtiaoyushun || this.skillName === EGeneralSkill.Zhongzhichengcheng || this.skillName === EGeneralSkill.Manzaiergui || this.skillName === EGeneralSkill.Fuzaoshengfeng){
            Core.Instance.generalData.assignedPassiveSkill = this.skillName
            console.log("设置被动武学Core.Instance.currentGeneralPassiveSkill:", Core.Instance.generalData.assignedPassiveSkill);
        }else{
            Core.Instance.generalData.assignedActiveSkill = this.skillName
            console.log("设置主动武学Core.Instance.currentGeneralActiveSkill:", Core.Instance.generalData.assignedActiveSkill);
        }

        this.ndBg.active = true; // 显示边框

        //Core.Instance.players.get(Core.Instance.currentPlayer).skillData.skills.get(Core.Instance.currentSkill as ESkill).runeId = this.runeId; // 设置当前武学的符文
        // 让自己的边框显现，让其他符文边框消失

        //this.node.getComponent(Sprite).color = color().fromHEX(EColor.Orange_Light); // 设置当前符文的颜色为绿色

        for (let skill of this.node.parent.children) {
            if (skill.name !== this.node.name) {
                skill.getChildByName('BG').active = false; // 隐藏其他符文的边框
            }
        }

        //Core.Instance.assignedGeneralSkills[this.node.name] = true; // 标记该武学已被分配
        Core.Instance.save(); // 保存数据
        //console.log("Core.Instance.players.get(Core.Instance.currentPlayer).skillData.skills.get(Core.Instance.currentSkill as ESkill).runeId:", Core.Instance.players.get(Core.Instance.currentPlayer).skillData.skills.get(Core.Instance.currentSkill as ESkill).runeId);


    }

    onBtnOpenSkillDetail(){
        // this.skillName = Core.Instance.currentSkill = (Core.Instance.currentPlayer + this.node.name ) as ESkill; // 假设节点名称是武学名称的一部分
        // console.log("点击了武学详情按钮", this.node.name);
        // if (this.ndSkillDetail) {
        //     this.ndSkillDetail.active = true;
        // } else {
        //     console.error("ndSkillDetail节点不存在");
        // }

    }


    // onBtnUpgrade() {
    //     this.playerData = Core.Instance.players.get(Core.Instance.currentPlayer);
    //     this.skillName = (Core.Instance.currentPlayer + this.node.name ) as ESkill; // 假设节点名称是武学名称的一部分
    //     this.skill = this.playerData.skillData.skills.get(this.skillName);
    //     console.log("点击了升级按钮");
    //     console.log("当前武学:", this.skill, "武学名称:", this.skillName, "玩家数据:", this.playerData);

    //     // 检查武学是否可以升级
    //     if (this.playerData.skillPoints <= 0) {
    //         console.error("武学点不足，无法升级武学");
    //         return;
    //     }


    //     // 检查武学是否达到最大等级
    //     console.log("当前武学等级:", this.skill.level, "最大武学等级:", this.skill.maxLevel);
    //     if (this.skill.level >= this.skill.maxLevel) {
    //         console.error("武学已达到最大等级，无法升级");
    //         return;
    //     }

    //     this.playerData.skillPoints -= 1; // 扣除一个武学点
    //     console.log("当前武学点:", this.playerData.skillPoints);

    //     // 这里可以添加升级武学的逻辑
    //     this.playerData.skillData.upgradeSkill(this.skillName);
    //     console.log("升级后的武学数据:", this.playerData.skillData.skills.get(this.skillName));


    //     Core.Instance.event.emit(EEvent.PlayerDataChange) // 通知其他组件更新武学点显示
        

    // }


}


